<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.taskName"
        type="text"
        placeholder="还原任务名"
        clearable
      />
      <el-select
        v-model="paramsForm.status"
        placeholder="请选择还原模式"
      >
        <el-option
          label="表级"
          value="table"
        />
        <el-option
          label="库级"
          value="database"
        />
      </el-select>
      <el-input
        v-model="paramsForm.restoreTimeNode"
        type="text"
        placeholder="还原时间节点"
        clearable
      />
      <el-select
        v-model="paramsForm.status"
        placeholder="状态"
        clearable
      >
        <el-option
          v-for="item in stateOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-date-picker
        v-model="paramsForm.timeRange"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00','23:59:59']"
        value-format="yyyy-MM-dd HH:mm:ss"
        clearable
        @change="(val)=>{
          if(!val){
            this.paramsForm.timeRange = []
          }
        }"
      />

      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >
        搜索
      </el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="resetForm"
      >
        重置
      </el-button>

      <el-dropdown
        style="margin-left: 0.6rem;"
        :disabled="!selection"
        @command="handleCommand"
      >
        <el-button
          type="primary"
          :disabled="!selection"
        >
          更多操作<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">

          <el-dropdown-item
            command="pause"
          >暂停</el-dropdown-item>
          <el-dropdown-item
            command="continue"
          >继续</el-dropdown-item>
          <el-dropdown-item
            command="push"
          >推送到集群</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      border
      @selection-change="handleSelectionChange"
      @select-all="()=>{
        if(selection){
          this.$refs.table.clearSelection()
        }
      }"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="任务名"
        prop="taskName"
        sortable
        fixed
        min-width="70px"
      />
      <el-table-column
        label="标识"
        prop="flag"
        sortable
        fixed
        min-width="70px"
      />
      <el-table-column
        label="还原模式"
        prop="restoreModel"
        sortable
        fixed
        min-width="60px"
      />
      <el-table-column
        label="库名"
        prop="dbName"
        sortable
      />
      <el-table-column
        label="表名列表"
        prop="tableNameList"
        width="90"
      >
        <template slot-scope="scope">
          <a
            style="color: dodgerblue"
            @click="() => {
              tableNameListDialogVis = true
              tbNameList = scope.row.tableNameList
            }
            "
          >查看</a>
        </template>
      </el-table-column>
      <el-table-column
        label="表Id列表"
        prop="tableIdList"
        width="90"
      >
        <template slot-scope="scope">
          <a
            style="color: dodgerblue"
            @click="() => {
              tableIdListDialogVis = true
              tableIdList = scope.row.tableIdList
            }
            "
          >查看</a>
        </template>
      </el-table-column>

      <el-table-column
        label="开始时间"
        prop="startTime"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="结束时间"
        prop="endTime"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="还原时间节点"
        prop="restoreTimeNode"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        label="状态"
        prop="status"
        sortable
        min-width="80px"
      >
        <template slot-scope="scope">
          <el-tag
            :type="getStateColor(scope.row.status)"
          >
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="任务阶段"
        prop="taskStage"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        fixed="right"
        label="表详情"
        min-width="60"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="() => {
              cbTaskId = scope.row.id
              cbRestoreListDialogVis = true
            }
            "
          >
            查看
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <!-- <ReRunDialog
      :dialog-visible="reRunDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        reRunDialogVisible = false
        getData()
      }"
    />
    <ContinueDialog
      :dialog-visible="continueDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        continueDialogVisible = false
        getData()
      }"
    /> -->

    <TableNameList
      :dialog-visible="tableNameListDialogVis"
      :table-name-list="tbNameList"
      @close="()=>{
        tableNameListDialogVis = false
      }"
    />

    <TableNameList
      :dialog-visible="tableIdListDialogVis"
      :table-name-list="tableIdList"
      @close="()=>{
        tableIdListDialogVis = false
      }"
    />

    <DetailDialog
      :dialog-visible="detailDialogVisible"
      :detail-data="detailData"
      @close="()=>{
        detailDialogVisible = false
      }"
    />

    <CBRestoreList
      :dialog-visible="cbRestoreListDialogVis"
      :task-id="cbTaskId"
      @close="()=>{
        cbRestoreListDialogVis = false
      }"
    />

  </div>
</template>

<script>
import cbApi from '@/api/coldStandby/index'
import TableNameList from './components/TableNameListDialog.vue'
import DetailDialog from './components/DetailDialog.vue'
import CBRestoreList from './components/CBRestoreList.vue'
// import dateUtil from '@/utils/date'

const form = {
  taskName: '',
  current: 1,
  size: 20,
  restoreModel: '',
  restoreTimeNode: '',
  status: '',
  timeRange: [

  ]
}

export default {
  components: {TableNameList, DetailDialog, CBRestoreList},
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      paramsForm: {...form},
      tableNameListDialogVis: false,
      tbNameList: '',
      tableIdListDialogVis: false,
      tableIdList: '',
      detailDialogVisible: false,
      detailData: {},
      cbRestoreListDialogVis: false,
      cbTaskId: '',
      selectedTaskId: '',
      selectedFlag: '',
      selectedName: '',
      selection: null,
      type: '统计',
      stateOption: [
        {
          label: '成功',
          value: '成功'
        },
        {
          label: '暂停',
          value: '完成'
        },
        {
          label: '失败',
          value: '失败'
        }

      ]
    }
  },
  created() {
    this.getData()
  },

  methods: {
    getStateColor(state) {
      if (state === '成功') {
        return 'success'
      } else if (state === '失败') {
        return 'danger'
      } else if (state === '暂停') {
        return ''
      }
    },
    getData(resetPage = true) {
      this.isLoading = true
      if (resetPage) {
        this.paramsForm.current = 1
      }
      const param = {
        startTime: this.paramsForm ? this.paramsForm.timeRange[0] : '',
        endTime: this.paramsForm.timeRange ? this.paramsForm.timeRange[1] : '',
        ...this.paramsForm
      }

      cbApi.cbRestoreTask(param)
        .then((res) => {
          this.data = res.records
          this.total = res.total
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    pause() {
      cbApi.pauseRestoreTask(this.selection.id)
      setTimeout(() => {
        this.$message.success('暂停成功')
        this.getData()
      }, 500)
    },
    continue() {
      cbApi.continueRestoreTask(this.selection.id)
      setTimeout(() => {
        this.$message.success('继续成功')
        this.getData()
      }, 500)
    },
    push() {
      cbApi.pushRestoreTask(this.selection.id)
      setTimeout(() => {
        this.$message.success('推送成功')
        this.getData()
      }, 500)
    },
    handleCommand(command) {
      this[command]()
    },
    resetForm() {
      this.paramsForm = {...form}
      this.getData()
    },
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val[val.length - 1])
      }
      this.selection = val[val.length - 1]
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
